package com.wlyuan.index.domain.source.assembler;

import com.wlyuan.core.marker.NamedUtils;
import com.wlyuan.core.marker.ValueUtils;
import com.wlyuan.index.api.dto.QuotationIndexDTO;
import com.wlyuan.index.domain.source.utils.SourceUtils;
import com.wlyuan.index.domain.source.valueobj.Quotation;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.var;
import org.apache.commons.collections4.CollectionUtils;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class QuotationAssembler {
    @Mapper(uses = {ValueUtils.class, NamedUtils.class, SourceUtils.class})
    public interface Assembler {
        @Mapping(target = "status", source = "relation.status")
        @Mapping(target = "tenantId", source = "relation.tenantId")
        @Mapping(target = "source", source = "source.subjectType")
        Quotation assemble(QuotationIndexDTO source, QuotationIndexDTO.Relation relation);
    }

    private static final Assembler ASSEMBLER = Mappers.getMapper(Assembler.class);

    public static List<Quotation> assemble(QuotationIndexDTO source) {
        if (CollectionUtils.isEmpty(source.getRelations())) {
            return Collections.emptyList();
        }
        var quotations = new ArrayList<Quotation>();
        source.getRelations().forEach(relation -> quotations.add(ASSEMBLER.assemble(source, relation)));
        return quotations;
    }
}
